//
//  MKLoginViewController.m
//  singForYou
//
//  Created by nicolas on 2017/3/7.
//  Copyright © 2017年 nicolas. All rights reserved.
//

#import "MKLoginViewController.h"
#import "MKBaseTabBarController.h"
#import "MBProgressHUD+MJ.h"
#import <MBProgressHUD.h>

@interface MKLoginViewController ()<UITextFieldDelegate>
//记住密码
@property (weak, nonatomic) IBOutlet UISwitch *rememberPwdSwitch;
//自动登录
@property (weak, nonatomic) IBOutlet UISwitch *autoLoginSwitch;
//登录
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
//账户文本框
@property (weak, nonatomic) IBOutlet UITextField *nameTf;
//密码文本框
@property (weak, nonatomic) IBOutlet UITextField *pwdTf;

@end

@implementation MKLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.nameTf becomeFirstResponder];

    self.nameTf.delegate = self;
    [self userDefaultRead];
    [self cancelKeyBoard];
}

//自动登录
- (IBAction)autoLogin:(UISwitch *)sender {
    if (self.autoLoginSwitch.on) {
        [self.rememberPwdSwitch setOn:YES animated:YES];
    }
}
//记住密码
- (IBAction)rememberPwd:(UISwitch *)sender {
    if (!self.rememberPwdSwitch.on) {
        [self.autoLoginSwitch setOn:NO animated:YES];
    }
}
//登录
- (IBAction)loginToMainVc:(UIButton *)sender {

    MBProgressHUD *Hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    Hud.labelText = @"正在登录";
    Hud.dimBackground = YES;
    
    __weak typeof (self) wself = self;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([[wself TrimWhiteSpace:wself.nameTf.text] isEqualToString:@"11"] && [[wself TrimWhiteSpace:wself.pwdTf.text] isEqualToString:@"22"]) {
            //偏好数据的写入
            [self userDefaultWrite];
            MKBaseTabBarController *tabBarController = [[MKBaseTabBarController alloc] init];
            [[UIApplication sharedApplication].delegate window].rootViewController = tabBarController;
            [MBProgressHUD showSuccess:@"登录成功"];
        }else {
            [MBProgressHUD showError:@"登录失败"];
        }
    });
}
//当账户文本框改变的时候触发
- (IBAction)nameDidChange:(UITextField *)sender {
    
    if ([self TrimWhiteSpace:self.nameTf.text].length>0 && [self TrimWhiteSpace:self.pwdTf.text].length>0) {
        self.loginBtn.enabled = YES;
    }else {
        self.loginBtn.enabled = NO;
    }
}
//当密码文本框改变的时候触发
- (IBAction)pwdDidChange:(UITextField *)sender {
    [self nameDidChange:self.pwdTf];
}

#pragma mark - Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.pwdTf becomeFirstResponder];
    return YES;
}

#pragma mark - Primary Method
- (NSString *)TrimWhiteSpace:(NSString *)text {
    NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];
    return [text stringByTrimmingCharactersInSet:set];
}
//收起键盘
- (void)cancelKeyBoard {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reKeyBoard)];
    [self.view addGestureRecognizer:tap];
}
- (void)reKeyBoard {
    [self.nameTf resignFirstResponder];
    [self.pwdTf resignFirstResponder];
}

//写入偏好
- (void)userDefaultWrite {
    //创建偏好写入对象
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //写入数据
    BOOL isRememberPwd = self.rememberPwdSwitch.on;
    BOOL isAutoLogin = self.autoLoginSwitch.on;
    //写入自动登录和记住密码的属性值
    [userDefault setBool:isRememberPwd forKey:@"isRememberPwdKey"];
    [userDefault setBool:isAutoLogin forKey:@"isAutoLoginKey"];
    if (isRememberPwd) {
        [userDefault setObject:self.nameTf.text forKey:@"nameKey"];
        [userDefault setObject:self.pwdTf.text forKey:@"passwordKey"];
    }
    
}
//读取偏好
- (void)userDefaultRead {
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    BOOL isRememberPwd = [userDefault boolForKey:@"isRememberPwdKey"];
    BOOL isAutoLogin = [userDefault boolForKey:@"isAutoLoginKey"];
    self.rememberPwdSwitch.on = isRememberPwd;
    self.autoLoginSwitch.on = isAutoLogin;
    //判断是否记住密码
    if (isRememberPwd) {
        self.nameTf.text = [userDefault objectForKey:@"nameKey"];
        self.pwdTf.text = [userDefault objectForKey:@"passwordKey"];
    }
    if (isAutoLogin) {
        [self loginToMainVc:self.loginBtn];
    }
    [self nameDidChange:self.nameTf];
}
@end
